<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-position="top" label-width="80px" size="mini">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="选择仓库" prop="consumeWareId">
            <treeselect v-model="form.consumeWareId" :normalizer="normalizer" :options="wareOptions"
                        placeholder="请选择"
                        @select="handleTreeSelectNode"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button :disabled="!form.consumeWareId" icon="el-icon-plus" plain size="mini" type="primary"
                     @click="handleAddCommodity">添加商品
          </el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="form.detailsList" border>
          <el-table-column align="center" label="商品类别" prop="commodityType"/>
          <el-table-column align="center" label="商品编号" prop="commodityNo"/>
          <el-table-column align="center" label="商品名称" prop="commodityName1"/>
          <el-table-column align="center" label="商品图片" prop="commodityImg" width="100">
            <template slot-scope="scope">
              <image-preview :height="50" :src="scope.row.commodityImg" :width="50"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="规格" prop="commoditySpec" width="80"/>
          <el-table-column align="center" label="单位" prop="unitName1" width="150"/>
          <el-table-column align="center" label="数量" prop="commodityNumber" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.commodityNumber"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="在存数量" prop="wareCommodityNumber"/>
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
            <template slot-scope="scope">
              <el-button v-hasPermi="['erp:ErpSupplierCommodity:remove']" icon="el-icon-delete" size="mini" type="text"
                         @click="handleDeleteCommodity(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-form>
    <div class="dialog-footer-options">
      <el-button type="primary" @click="submitForm">保存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

    <!-- 选择商品 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isCommodityOpen" append-to-body title="选择商品"
               width="80%">
      <ErpCommodity v-if="isCommodityOpen" :notCommodityIds="notCommodityIds" :wareId="form.consumeWareId"
                    @SelectionChange="handleSelectionChange">
      </ErpCommodity>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSelectedCommdity">确 定</el-button>
        <el-button @click="isCommodityOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {addMaterialConsume, getMaterialConsume, updateMaterialConsume} from "@/api/cld/materialConsume";
import {listErpWareHouse} from "@/api/erp/ErpWareHouse";
import ErpCommodity from '@/views/erp/ErpCommodity/selectedCommodity'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  components: {
    ErpCommodity,
    Treeselect
  },
  props: [],
  data() {
    return {
      // 调出仓库
      wareOptions: [],
      // 选择商品页面
      isCommodityOpen: false,
      form: {},
      rules: {
        consumeWareId: [
          {required: true, message: "仓库不能为空", trigger: "blur"}
        ],
      },
      // 仓库id
      wareId: null
    }
  },
  created() {
    this.reset();
    this.getTreeselect();
    const materialConsumeId = this.$route.params && this.$route.params.materialConsumeId;
    if (materialConsumeId != 0) {
      this.getMaterialConsumeInfo(materialConsumeId);
    } else {
      this.loading = false;
    }
  },
  mounted() {

  },
  methods: {
    // 获取详情
    getMaterialConsumeInfo(id) {
      getMaterialConsume(id).then(response => {
        this.form = response.data;
      });
    },
    /** 转换仓库管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.wareId,
        label: node.wareName,
        children: node.children
      };
    },
    /** 查询仓库管理下拉树结构 */
    getTreeselect() {
      listErpWareHouse({
        params: {
          notIdentifier: 2
        }
      }).then(response => {
        this.wareOptions = this.handleTree(response.data, "wareId", "warePid");
      });
    },
    // 仓库选择
    handleTreeSelectNode(e) {
      if (e.staffId) {
        this.form.projectId = e.projectId
        this.form.consumeType = 1;
      } else {
        this.$message.error('请选择个人仓！');
        this.form.consumeWareId = null;
      }
    },
    // 选择商品
    handleAddCommodity() {
      // 设置商品过滤id
      this.notCommodityIds = this.form.detailsList?.map(item => item.commodityId);
      this.isCommodityOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialConsumeId != null) {
            updateMaterialConsume(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              const obj = {path: "/cloudManage/supplies/materialConsume", name: "materialConsume"};
              this.$tab.closeOpenPage(obj);
              this.$tab.refreshPage(obj);
            });
          } else {
            addMaterialConsume(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              const obj = {path: "/cloudManage/supplies/materialConsume", name: "materialConsume"};
              this.$tab.closeOpenPage(obj);
              this.$tab.refreshPage(obj);
            });
          }
        }
      });
    },
    // 取消
    cancel() {
      const obj = {path: "/cloudManage/supplies/materialConsume", name: "materialConsume"};
      this.$tab.closeOpenPage(obj);
      this.$tab.refreshPage(obj);
    },
    // 商品选择
    handleSelectionChange(selection) {
      this.temporyList = selection;
    },
    // 确定选择商品
    submitSelectedCommdity() {
      this.form.detailsList = this.temporyList;
      this.isCommodityOpen = false;
    },
    /** 删除商品 */
    handleDeleteCommodity(row) {
      const index = this.form.detailsList.indexOf(row);
      if (index > -1) {
        this.form.detailsList.splice(index, 1);
      }
    },
    // 表单重置
    reset() {
      this.form = {
        materialConsumeId: null,
        consumeOdd: null,
        consumeWareId: null,
        consumeId: null,
        consumeTime: null,
        consumeType: null,
        consumeDesc: null,
        consumeImgs: null,
      };
      this.resetForm("form");
    },
  }
}

</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
